//
//  NewFeatureController.swift
//  weibo
//
//  Created by mac on 15/12/16.
//  Copyright © 2015年 rong. All rights reserved.
//

import UIKit
import SnapKit

private let imageCount = 4
private let reuseIdentifier = "cell"
class NewFeatureController: UICollectionViewController {
//不需要添加override
    //构造方法只能向上继承一个层级 不能够透过父类 去找爷爷的构造方法
    init() {
    //调用指定的构造方法 实例化对象
        //指定流水布局
        let layout = UICollectionViewFlowLayout()
        //修改布局
        layout.itemSize = UIScreen.mainScreen().bounds.size
        //修改视图滚动方法
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //设置item上下左右的间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        super.init(collectionViewLayout: layout)
        //suo.init是构造的最后一步
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        //取消弹簧效果
        collectionView?.bounces = false
    }
    //如果当前视图控制器通过xib/sb加载 程序就会崩溃
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView?.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageCount
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        //真是但身份是NewfeatureCell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        cell.index = indexPath.item
        return cell
    }
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //cell完成展示之后 indexPath对应的是上一个cell indexPath不能使用indexPath来获取cell
        print(indexPath.item)
        //获取精准的cell
        let cell = collectionView.visibleCells().last! as! NewFeatureCell
        //获取cell的indexPath
        let index = collectionView.indexPathForCell(cell)!
        //判断index。item是不是等于3 执行动画
        if index.item == imageCount - 1 {
        //执行动画
            cell.showAnimation()
        }
        
    }
    
}
//在一个文件中 可以同时创建多个类
class NewFeatureCell: UICollectionViewCell {
//添加属性
    var index: Int = 0 {
        didSet {
        //和OC重写set方法一样
            //修改cell对应显示的image
            iconView.image = UIImage(named: "new_feature_\(index + 1)")
            //隐藏开始按钮
            startBtn.hidden = true
        }
    }
    private func showAnimation() {
    //在动画开始之前 修改transfrom
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        startBtn.hidden = false
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("OK")
        }
    }
    @objc private func startBtnDidClick() {
    print(__FUNCTION__)
        //动画结束的时候 发出页面切换的通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
    }
    //重写构造方法 cell构造入口
    override init(frame: CGRect) {
        super.init(frame: frame)
        print(frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置界面
    private func setupUI() {
    //添加子视图
        //自定义的子视图 需要添加在contentView上
        contentView.addSubview(iconView)
        contentView.addSubview(startBtn)
        //使用snapkit添加约束
        //snp_makeConstraints 表示添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            //make表示需要添加约束的对象
            make.edges.equalTo(contentView.snp_edges)
        }
        //设置开始按钮的约束
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView.snp_bottom).offset(-180)
        }
        //添加按钮的点击事件
        startBtn.addTarget(self, action: "startBtnDidClick", forControlEvents: .TouchUpInside)
    }
    //分析视图中所有的子视图 将所有的子视图懒加载出来
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    private lazy var startBtn: UIButton = {
    let btn = UIButton()
       btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        btn.setTitle("开始体验", forState: .Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        btn.sizeToFit()
        return btn
    }()
}